<template>
  <a-card class="card_wrap">
    <div class="title_box">设备</div>

    <div class="devList_box">
      <a-list :data-source="props.devDataList" style="height: 280px; overflow-y: scroll">
        <template #renderItem="{item}">
          <a-list-item>
            <div class="dev_item_box">
              <a-checkbox v-model:checked="item.isChecked" @change="devCheckChange(item)">{{
                item.devName
              }}</a-checkbox>
            </div>
          </a-list-item>
        </template>
        <template #header>
          <div class="dev_list_header_box">
            <a-checkbox v-model:checked="isDevCheckAll" @change="checkAllChange">
              <div class="dev_list_header_box_checkal_content_box">
                <span class="checkedAll_span">全选以下所有设备</span>
                <img
                  v-if="isDevCheckAll"
                  src="https://d.lohand.com:1112/images/ConstantBlueCloud/WEB/zhengque_icon.png"
                />
                <img
                  v-else
                  src="https://d.lohand.com:1112/images/ConstantBlueCloud/WEB/cuowu_icon.png"
                />
              </div>
            </a-checkbox>
          </div>
        </template>
      </a-list>
    </div>

    <!-- <div class="sub_btn_box">
      <a-tooltip placement="top">
        <template #title>
          <span>提示:请确保参数项除备注外不可为空,设备选择数量必须大于0!</span>
        </template>
        <a-button
          @click="emit('submitParamStandardClick')"
          type="primary"
          :disabled="props.isDisabled"
          >保存</a-button
        >
      </a-tooltip>
    </div> -->
  </a-card>
</template>

<script lang="ts">
  import {defineComponent, ref, onMounted, defineProps} from 'vue'
  export default defineComponent({
    name: 'DevCard'
  })
</script>
<script lang="ts" setup>
  // 引入
  import {
    standardSelectType,
    standardSelectTypeList,
    paramType,
    paramTypeList,
    devType,
    devTypeList,
    paramStandard,
    paramStandardList
  } from '../../../standard/module/paramCard/type.ts'

  // props数据
  const props = defineProps(['devDataList', 'devIds'])

  // emits事件
  const emit = defineEmits(['updateDevIdsFunc'])

  // 单个设备的Change事件
  const devCheckChange = (devItem: devType) => {
    compIsCheckedAll()
    // 更改ids
    compIdsStrFunc()
  }

  // 计算 单选某一个后 是不是全选状态
  const compIsCheckedAll = () => {
    // 选中的数量
    let isCheckedNum = 0
    props.devDataList.forEach((item: devType) => {
      if (item.isChecked) {
        isCheckedNum += 1
      }
    })
    if (isCheckedNum == props.devDataList.length) {
      isDevCheckAll.value = true
    } else {
      isDevCheckAll.value = false
    }
  }

  // 计算ids字符串的函数
  const compIdsStrFunc = () => {
    let idsArr = []
    props.devDataList.forEach((item: devType) => {
      if (item.isChecked) {
        idsArr.push(item.devId)
      }
    })
    let idsStr = idsArr.join(',')
    emit('updateDevIdsFunc', idsStr)
  }

  // 全选的Change事件
  const checkAllChange = (e: object) => {
    console.log(e.target.checked)
    let flag = e.target.checked

    props.devDataList.forEach((item: devType) => {
      if (flag) {
        item.isChecked = true
      } else {
        item.isChecked = false
      }
    })

    // 更改ids
    compIdsStrFunc()
    // props.isDevCheckAll = !props.isDevCheckAll
  }

  // 设备列表是否全选的标识
  const isDevCheckAll = ref(false)
  // 初始化
  onMounted(() => {
    // console.log(props.devDataList)
  })
</script>

<style lang="less" scoped>
  ::v-deep .ant-card-body {
    padding: 20px 0px 20px 0px;
  }
  .card_wrap {
    // width: 500px;

    .title_box {
      padding: 3px 0px 3px 20px;
      font-size: 18px;
      letter-spacing: 2px;
      font-weight: bold;
      color: #324569;
      margin-bottom: 15px;
      background-image: url(https://d.lohand.com:1112/images/ConstantBlueCloud/WEB/card-vein-inline.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }

    .devList_box {
      padding: 0px 20px;

      .dev_list_header_box {
        .dev_list_header_box_checkal_content_box {
          line-height: 22px;
          display: flex;
          .checkedAll_span {
            font-weight: 500 !important;
            font-family: kaigengo-medium !important;
            --tw-text-opacity: 1 !important;
            color: rgb(41 48 53 / var(--tw-text-opacity)) !important;
          }

          img {
            margin: 2.5px 0px 0px 6px;
            width: 16px;
            height: 16px;
          }
        }

        // border-bottom: 1px solid black;
        // margin-bottom: 10px;
      }
    }

    .sub_btn_box {
      padding: 0px 20px;
    }
  }
</style>
